package com.ezlearning.platform.services.core.impl;

import com.ezlearning.platform.dto.StudentDto;
import com.ezlearning.platform.model.Student;
import com.ezlearning.platform.repositories.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {

    private StudentRepository studentRepository;

    @Autowired
    public StudentService(StudentRepository studentRepository) {
        this.studentRepository = studentRepository;
    }

    public void create(StudentDto studentDto) {
        String nombre = studentDto.getNombre();
        String apellido = studentDto.getApellido();
        String correo = studentDto.getCorreo();
        String descripcion = studentDto.getDescripcion();
        String imgurl = studentDto.getImgurl();
        Student student = new Student(nombre, apellido, correo, descripcion, imgurl);
        studentRepository.save(student);
    }

    public List<Student> getAll() {
        return studentRepository.findAll();
    }

    public void update(Student student) {
        Student currentStudent = studentRepository.findById(student.getId_student()).get();

        currentStudent.setNomStudent(student.getNomStudent());
        currentStudent.setApeStudent(student.getApeStudent());
        currentStudent.setCorreoStudent(student.getCorreoStudent());
        currentStudent.setDescStudent(student.getDescStudent());
        currentStudent.setImgurl(student.getImgurl());

        studentRepository.save(currentStudent);
    }

    public void patch(Student student) {
        Student current = studentRepository.findById(student.getId_student()).get();

        current.setDetalleStudent(student.getDetalleStudent());

        studentRepository.save(current);
    }

    public void delete(Student student) {
        studentRepository.delete(student);
    }

}
